/**
 * 行政区划
 */
$.ajaxSetup({
	cache : false,
	async : false
});
(function($) {
	$.fn.extend({
		loadAreaSel:function(options){
			var _div = "<div></div>";
			var _ul = "<ul></ul>";
			var _li = "<li></li>";
			this.each(function(){
				var _this = $(this);
				_this.attr("readonly","readonly");
				var areaSelParent = $(_div).addClass('areaSelParent')
				//.css({display: _this.css('display')})
				.mouseleave(function(){
					areaSel.hide();
				});
				_this.wrap(areaSelParent);
				var areaSel = $(_div).addClass('areaSel');
				var areaTop_sheng = $(_li).addClass('on').text('省份').click(function(){turnOn($(this));turnOn(areaList_sheng);});
				var areaTop_shi = $(_li).text('城市').click(function(){turnOn($(this));turnOn(areaList_shi);});
				var areaTop_qu = $(_li).text('县/区').click(function(){turnOn($(this));turnOn(areaList_qu);});
				var areaTop = $(_ul).addClass('areaTop').append(areaTop_sheng).append(areaTop_shi).append(areaTop_qu);
				var areaList_sheng = $(_ul).addClass('areaList on');
				var areaList_shi = $(_ul).addClass('areaList');
				var areaList_qu = $(_ul).addClass('areaList');
				loadAreaList(1,areaList_sheng);
				var areaList = $(_div).append(areaList_sheng).append(areaList_shi).append(areaList_qu);
				areaSel.append(areaTop).append(areaList);
				_this.after(areaSel);
				_this.click(function() {
					areaSel.show();
				});
				
				function loadAreaList(id,areaList){
					var basePath = $("#basePath").val();
					var listUrl = basePath+"/area/List?pid="+id;
					//var dataUrl = basePath+"/area/Data.do";
					areaList.empty();
					$.getJSON(listUrl,function(data){
						if(data != null){
							$.each(data,function(key,item){
								var areaLi = $(_li).attr('id',item.id).attr('title',item.pingyin).text(item.name).click(function(){
									areaClick($(this),item.level);
								});
								areaList.append(areaLi);
							});
						}
					});
				};
				
				function areaClick(areaLi,level){
					var id = areaLi.attr('id');
					var name = areaLi.text();
					turnOn(areaLi);
					if(level == 2){
						areaTop_sheng.text(name).attr('id',id);
						var provinece= options.provinece;
						$("#"+provinece+"").attr("value",id);
						areaTop_shi.text('城市');
						areaTop_qu.text('县/区');
						loadAreaList(id,areaList_shi);
						turnOn(areaTop_shi);
						turnOn(areaList_shi);
					}
					if(level == 3){
						areaTop_shi.text(name).attr('id',id);
						var city= options.city;
						$("#"+city+"").attr("value",id);
						areaTop_qu.text('县/区');
						loadAreaList(id,areaList_qu);
						turnOn(areaTop_qu);
						turnOn(areaList_qu);
					}
					if(level == 4){
						areaTop_qu.text(name).attr('id',id);
						var countyArea= options.countyArea;
						$("#"+countyArea+"").attr("value",id);
						var fullName = areaTop_sheng.text()+"-"+areaTop_shi.text()+"-"+name;
						_this.val(fullName);
						areaSel.hide();
					}
				};
				
				function turnOn(o){
					o.siblings('.on').removeClass('on');
					o.addClass('on');
				};
				function turnShow(o){
					o.siblings.hide();
					o.show();
				};
			});
		}
	});
})(jQuery);